package com.xmh.myutil.othertest;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;

//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration("classpath:spring_bean.xml")
public class OracleTest {

	@Resource(name = "dataSource")
	private ComboPooledDataSource comboPooledDataSource;

	@Value("${driverClass}")
	private String driverClass;

	@Value("${jdbcUrl}")
	private String jdbcUrl;

	@Value("${user}")
	private String user;

	@Value("${password}")
	private String password;

	@Value("${initialPoolSize}")
	private String initialPoolSize;

	@Value("${maxPoolSize}")
	private String maxPoolSize;

	@Value("${minPoolSize}")
	private String minPoolSize;

	private static String USERNAMR = "HIS_WB";
	private static String PASSWORD = "HIS_WB";
	private static String DRVIER = "oracle.jdbc.OracleDriver";
	private static String URL = "jdbc:oracle:thin:@10.32.9.181:1521:ORCL";

	// 创建一个数据库连接
	static Connection connection = null;
	// 创建预编译语句对象，一般都是用这个而不用Statement
	static PreparedStatement pstm = null;
	// 创建一个结果集对象
	static ResultSet rs = null;

	public static void main(String[] args) {
		ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
		try {
			comboPooledDataSource.setDriverClass(DRVIER);
			comboPooledDataSource.setUser(USERNAMR);
			comboPooledDataSource.setPassword(PASSWORD);
			comboPooledDataSource.setJdbcUrl(URL);

			connection = comboPooledDataSource.getConnection();
			System.out.println("成功连接数据库");
		} catch (Exception e) {
			e.printStackTrace();
		}
//		getConnection();
		SelectData();
	}
//
//	@Test
//	public void myBatis() {
//		TransactionFactory transactionFactory = new JdbcTransactionFactory();
//
//		Environment environment = new Environment("development", transactionFactory, comboPooledDataSource);
//
//		Configuration configuration = new Configuration(environment);
//
//		configuration.addMapper(TestDao.class);
//
//		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
//
//		SqlSession session = sqlSessionFactory.openSession();
//
//		TestDao mapper = session.getMapper(TestDao.class);
//
//		List<Map<String, Object>> selectAll = mapper.selectAll();
//
//		System.out.println(selectAll);
//
//		session.close();
//	}

	private static void getConnection() {
		try {
			Class.forName(DRVIER);
			connection = DriverManager.getConnection(URL, USERNAMR, PASSWORD);
			System.out.println("成功连接数据库");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void SelectData() {
		String sql = "select * from DATA_PRESCRIPTION  ";
		try {
			pstm = connection.prepareStatement(sql);
			rs = pstm.executeQuery();
			while (rs.next()) {
				String id = rs.getString("id");

				System.out.println(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
